package com.djzhou.gmms.service.log.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import jodd.util.StringUtil;

import com.djzhou.gmms.model.company.TbCompany;
import com.djzhou.gmms.model.department.TbDepartment;
import com.djzhou.gmms.model.entity.PageBean;
import com.djzhou.gmms.model.log.TbLog;
import com.djzhou.gmms.model.user.TbUser;
import com.djzhou.gmms.model.user.VoResource;
import com.djzhou.gmms.service.base.impl.BaseServiceImpl;
import com.djzhou.gmms.service.log.LogService;
import com.djzhou.gmms.util.session.UserSession;
import com.djzhou.util.DateUtil;
import com.mysql.jdbc.log.NullLogger;

public class LogServiceImpl extends BaseServiceImpl implements LogService{

	@Override
	public void add(TbLog t) {
		
		
	}

	@Override
	public void delete(Integer id) {
		TbLog log=logDAO.queryByID(TbLog.class, id);
		log.setDeleteFlag(1);
		logDAO.update(log);
		
	}

	@Override
	public TbLog queryById(Integer id) {
		return logDAO.queryByID(TbLog.class, id);
	}

	@Override
	public List<TbLog> queryList(HashMap<String, Object> hashMap) {
		
		return null;
	}

	@Override
	public PageBean<TbLog> queryPageBean(HashMap<String, Object> hashMap,
			int pageSize, int currentPage) {
		String hql="from TbLog log where log.deleteFlag=0";
		StringBuffer sb=new StringBuffer();
		sb.append(hql);
		if(hashMap!=null && hashMap.size()!=0){
			
//			if(UserSession.getSessionModel().getOpenComFlag()){
//				if(hashMap.get("companyId")!=null && !StringUtil.isEmpty(hashMap.get("companyId").toString())){
//					sb.append(" and log.company.companyId in(").append(hashMap.get("companyId").toString()).append(")");
//				}
//			}else{
//				if(hashMap.get("departmentId")!=null && !StringUtil.isEmpty(hashMap.get("departmentId").toString())){
//					sb.append(" and log.department.departmentId in(").append(hashMap.get("departmentId").toString()).append(")");
//				}
//			}
			
			if(hashMap.get("menuName")!=null){
				sb.append(" and log.menuName like '%").append(hashMap.get("menuName").toString()).append("%'");
			}
			if(hashMap.get("recordId")!=null){
				sb.append(" and log.opWay like '%").append(hashMap.get("recordId").toString()).append("%'");
			}
			if(hashMap.get("startDate")!=null){
				sb.append(" and log.createTime>='").append(hashMap.get("startDate").toString()+"'");
			}
			if(hashMap.get("endDate")!=null){
				sb.append(" and log.createTime<='").append(DateUtil.turnDateToString(DateUtil.getAfterDate(DateUtil.turnStringToDate(hashMap.get("endDate").toString(), "yyyy-MM-dd"), 1), "yyyy-MM-dd")+"'");
			}
			if(hashMap.get("loginName")!=null){
				sb.append(" and log.user.loginName like '%").append(hashMap.get("loginName").toString()).append("%'");
			}
		}
		sb.append(" order by createTime desc");
		hql=sb.toString();
		PageBean<TbLog> pageBean=logDAO.selectByPage(hql, pageSize, currentPage);
		return pageBean;
	}

	@Override
	public void update(TbLog t) {
		
		
	}

	@Override
	public void addLog(String menuName, TbUser user, String opWay,
			Integer keyId, String objectName, TbCompany company,TbDepartment department) {
		TbLog log=new TbLog();
		log.setKeyId(keyId);
		log.setCompany(company);
		log.setUser(user);
		log.setBackupData("");
		log.setOpWay(opWay);
		log.setCreateTime(new Date());
		log.setObjectName(objectName);
		log.setMenuName(menuName);
		log.setDepartment(department);
		StringBuffer sb=new StringBuffer();
		sb.append(user.getRealName()).append("(登录名:").append(user.getLoginName()).append(")").append("在").append(DateUtil.turnDateToString(log.getCreateTime(), "yyyy年MM月dd日HH时mm分ss秒")).append("通过").append(log.getMenuName()).append("执行了").append(log.getOpWay()).append("操作,记录ID为").append(log.getKeyId()!=null?log.getKeyId():"无");
		log.setRemark(sb.toString());
		log.setDeleteFlag(0);
		System.out.println(log.getRemark());
		logDAO.insert(log);
	}
	@Override
	public VoResource query4AutoComplete(HashMap<String, Object> hashMap, String sidx,
			String sord, int pageSize, int currentPage) {
		// TODO Auto-generated method stub
		return null;
	}
}
